package com.gandong8.trace.tool.util;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class ZKCuratorUtil {

    private static CuratorFramework curatorFramework = null;

    public static void clientInit(String zkAddress, String service) {
        if (curatorFramework != null) {
            return;
        }
        if(zkAddress.contains("/")) {
            String[] arr = zkAddress.split("/");
            zkAddress = arr[2];
        }
        if(zkAddress.contains("?")) {
            zkAddress = zkAddress.split("\\?")[0];
        }
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
        curatorFramework = CuratorFrameworkFactory.builder().connectString(zkAddress)
                .sessionTimeoutMs(10000)
                .retryPolicy(retryPolicy)
                .namespace("dubbo/"+service)
                .build();
        curatorFramework.start();
    }

    private static List<String> getChildren(String path, boolean watch)  {
        try {
            return watch ? curatorFramework.getChildren().watched().forPath(path) : curatorFramework.getChildren().forPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static List<String> getConsumerIpList() {
        String path = "/consumers";
        List<String> list =  getChildren(path, false);
        return parseNode(list);
    }

    public static List<String> getProviderIpList() {
        String path = "/providers";
        List<String> list =  getChildren(path, false);
        return parseNode(list);
    }

    private static List<String> parseNode(List<String> list) {
        List<String> ipList =  new ArrayList<>();
        for(String data:list) {
            String str ;
            try {
                str = URLDecoder.decode(data, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                continue;
            }
            String[] tmp = str.split("/");
            if(tmp.length < 3) {
                continue;
            }
            ipList.add(tmp[2].split(":")[0]);
        }
        return ipList;
    }

    private static byte[] getNodeData(String path){
        try {
            curatorFramework.getData().forPath(path);
            return curatorFramework.getData().forPath(path);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void close() {
        curatorFramework.close();
    }

    /*
    use zkclient
    <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
        </dependency>
    public static List<String> getIpList(String zkAddress, String service) {
        try {
            if(zkAddress.contains("/")) {
                String[] arr = zkAddress.split("/");
                zkAddress = arr[2];
            }
            if(zkAddress.contains("?")) {
                zkAddress = zkAddress.split("\\?")[0];
            }
            ZkClient zkClient = new ZkClient(zkAddress, 3000);
            List<String> list = zkClient.getChildren("/dubbo/" + service + "/consumers");
            return parseNode(list);
        } catch (ZkNoNodeException z) {

        } catch (Exception e) {

        }
        return null;
    }

     */



}
